package com.liangzhong.model.goods
{
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.model.goods.vo.AttributeVo;
	import com.liangzhong.model.goods.vo.DrugVo;
	import com.liangzhong.model.goods.vo.FootwearVo;
	import com.liangzhong.model.goods.vo.GoodsVo;
	import com.liangzhong.model.goods.vo.MaterialVo;
	import com.liangzhong.model.goods.vo.OtherGoodVo;
	import com.liangzhong.model.goods.vo.PlayerStarVo;
	import com.liangzhong.model.goods.vo.TrikotVo;
	import com.liangzhong.model.type.GoodsType;
	
	import flash.utils.getTimer;

	/**
	 *物品管理器 
	 * @author lake
	 * 
	 */	
	public class GoodsManager
	{
		/**
		 * 物品列表 包含所有物品
		 * 由物品描述表解析
		 */		
		public var goodsList 		:Array = [];
		/**
		 *球衣 
		 */		
		public var trikots			:Array = [];
		/**
		 *球星 
		 */		
		public var playerStars		:Array = [];
		/**
		 *属性卡 
		 */		
		public var attributes		:Array = [];
		/**
		 *鞋袜类 
		 */		
		public var footwears		:Array = [];
		/**
		 * 材料
		 */		
		public var materials		:Array = [];
		/**
		 *其他商品 
		 */		
		public var others			:Array = [];
		/**
		 *药剂 
		 */		
		public var drugs			:Array = [];
		
		public function GoodsManager()
		{
		}
		/**
		 *解析物品列表 
		 */		
		public function parseGoodsList():void
		{
			//var tempTime :Number = getTimer();
			parseDatas(ResourceConst.GOODS_TRIKOT,trikots,TrikotVo,"tb_trikot");
			parseDatas(ResourceConst.GOODS_PAYERSTAR,playerStars,PlayerStarVo,"tb_star");
			parseDatas(ResourceConst.GOODS_ATTRIBUTE,attributes,AttributeVo,"tb_attribute");
			parseDatas(ResourceConst.GOODS_FOOTWEAR,footwears,FootwearVo,"tb_footwear");
			parseDatas(ResourceConst.GOODS_MATERIAL,materials,MaterialVo,"tb_stuff");
			parseDatas(ResourceConst.GOODS_OTHER,others,OtherGoodVo,"tb_other");
			parseDatas(ResourceConst.GOODS_DRUG,drugs,DrugVo,"tb_drug");
			//trace("parseGoodsList use time:",getTimer() - tempTime);
		}
		/**
		 *解析数据 
		 * @param resKey 资源key
		 * @param dataCons 对应类商品数组
		 * @param goodClass 对象类
		 * 
		 */		
		private function parseDatas(resKey :String,
									dataCons:Array,
									goodClass :Class,
									xmlRoot :String = "fb"):void
		{
			var xmlGoods:XML = ResourceManager.instance.getResource(resKey);
			if(xmlGoods == null)
			{
				return;
			}
			var goodsVo :GoodsVo;
			for each(var item:XML in xmlGoods["" + xmlRoot])
			{
				goodsVo = new goodClass();
				goodsVo.parse(item);
				goodsList.push(goodsVo);
				dataCons.push(goodsVo);
			}
		}
		/**
		 *根据id得到球衣 
		 * @param id
		 * @return 
		 */		
		public function getTrikotById(id :int):GoodsVo
		{
			for each(var goods :GoodsVo in trikots)
			{
				if(goods.pid == id)
				{
					return goods;
				}
			}
			return null;
		}
		/**
		 *根据id得到球星
		 * @param id
		 * @return 
		 */		
		public function getStarById(id :int):GoodsVo
		{
			for each(var goods :GoodsVo in playerStars)
			{
				if(goods.pid == id)
				{
					return goods;
				}
			}
			return null;
		}
		/**
		 *根据id得到属性卡
		 * @param id
		 * @return 
		 */		
		public function getAttributeById(id :int):GoodsVo
		{
			for each(var goods :GoodsVo in attributes)
			{
				if(goods.pid == id)
				{
					return goods;
				}
			}
			return null;
		}
		/**
		 *根据id得到药剂
		 * @param id
		 * @return 
		 */		
		public function getDrugById(id :int):GoodsVo
		{
			for each(var goods :GoodsVo in drugs)
			{
				if(goods.pid == id)
				{
					return goods;
				}
			}
			return null;
		}
		/**
		 *根据id得到鞋袜
		 * @param id
		 * @return 
		 */		
		public function getFootWearById(id :int):GoodsVo
		{
			for each(var goods :GoodsVo in footwears)
			{
				if(goods.pid == id)
				{
					return goods;
				}
			}
			return null;
		}
		/**
		 *根据id得到材料
		 * @param id
		 * @return 
		 */		
		public function getMaterialById(id :int):GoodsVo
		{
			for each(var goods :GoodsVo in materials)
			{
				if(goods.pid == id)
				{
					return goods;
				}
			}
			return null;
		}
		/**
		 *根据id得到其他类商品
		 * @param id
		 * @return 
		 */		
		public function getOtherById(id :int):GoodsVo
		{
			for each(var goods :GoodsVo in others)
			{
				if(goods.pid == id)
				{
					return goods;
				}
			}
			return null;
		}
		/**
		 *获取物品 通过类型 和 id 
		 * @param type 如4 表示球衣
		 * @param id 如1 表示对应表中的数据
		 * @return 
		 */		
		public function getGoodsByTypeAndId(type:int,id:int):GoodsVo
		{
			var goods :GoodsVo;
			switch(type)
			{
				case GoodsType.TEAMCLOTH://球衣
				{
					goods = getTrikotById(id);
					break;
				}
				case GoodsType.PLAYERCARD://球星
				{
					goods = getStarById(id);
					break;
				}
				case GoodsType.ATTRIBUTE://属性卡
				{
					goods = getAttributeById(id);
					break;
				}
				case GoodsType.DRUG://药剂
				{
					goods = getDrugById(id);
					break;
				}
				case GoodsType.FOOTWEAR://鞋袜
				{
					goods = getFootWearById(id);
					break;
				}
				case GoodsType.MATERIAL://材料
				{
					goods = getMaterialById(id);
					break;
				}
				case GoodsType.OTHER://其他类
				{
					goods = getOtherById(id);
					break;
				}
			}
			return goods;
		}
		/**
		 *获取商品通过两种类型和品质 
		 * @param type 大类型 如属性卡
		 * @param ntype 小类型 如进攻卡
		 * @param color 品质 1 - 7
		 * @return 
		 */		
		public function getGoodsByTypeNtypeAndColor(type:int,
													ntype:int,
													color:int):GoodsVo
		{
			for each(var goods :GoodsVo in goodsList)
			{
				if(goods.type == type 
				&&goods.ntype == ntype
				&&goods.color == color)
				{
					return goods;
				}
			}
			return null;
		}
	}
}